$("<link>").attr({
	rel : "stylesheet",
	type : "text/css",
	href : "./Public/css/validationEngine.jquery.css"
}).appendTo("head");

var ValidationObj = {};

ValidationObj.submit_btn = '';

ValidationObj.beforeCall = function() {

	ValidationObj.submit_btn = $('.submit_btn').button('loading');

	var result = true;
	if (window.beforePost && typeof (window.beforePost) == "function") {
		result = window.beforePost();
	}
	return result;
}

ValidationObj.ajaxValidationCallback = function(status, form, json, options) {
	if (json.info) {
		layer.msg(json.info);
	}

	if (json.status == 0) {
		ValidationObj.submit_btn.button('reset');
	} else {
		if (json.url) {
			setTimeout(function() {
				location.href = json.url;
			}, 1500);
		}
	}
}

$.getScript('./Public/js/jquery.validationEngine.js', function() {
	$.getScript('./Public/js/jquery.validationEngine-zh_CN.js', function() {
		$('.validation').validationEngine('attach', {
			validateNonVisibleFields : true,
			showPrompts : false,
			maxErrorsPerField : 1,
			ajaxFormValidation : true,
			ajaxFormValidationMethod : 'post',
			onAjaxFormComplete : ValidationObj.ajaxValidationCallback,
			onBeforeAjaxFormValidation : ValidationObj.beforeCall,
			onFieldFailure : function(field) {
				if ($('.nav-tabs').length > 0) {
					var target_id = $(field).closest('.tab-pane').attr('id');
					$('.nav-tabs').find('.' + target_id).trigger('click');
				}
				$(field).closest('.form-group').addClass('has-error');
			},
			onFieldSuccess : function(field) {
				$(field).closest('.form-group').removeClass('has-error');
			}
		});
	});
});
